<?php

App::uses('AppController', 'Controller');

class PagesController extends AppController {

    public function index() {
        $slideshow = array();
        $this->loadModel('Group');
        $this->loadModel('Product');
	    
        $groups = $this->Group->find('list');
        foreach ($groups as $groupId =>$groupName) {
            $product = $this->Product->find('first', array(
                'conditions' => array(
                    'Product.group_id' => $groupId,
                    'Product.image !=' => '',
                    'Product.image !=' => null
                ),
                'recursive'  => 0,
                'order'      => 'RAND()'
            ));
            if (! empty($product)) {
                $productId    = $product['Product']['id'];
                $productName  = $product['Product']['name'];
                $productImage = $product['Product']['image'];
                $groupId      = $product['Group']['id'];
                $groupName    = $product['Group']['name'];
                
                $slideshow[] = array(
                    'title' => "{$groupName}: {$productName}",
                    'link'  => "/proizvod/{$productId}/" . urlize($groupName) . "/" . urlize($productName),
                    'image' => "products/{$groupId}/{$productImage}"
                );
            }
        }
        
	    $this->set(compact('slideshow'));
	}
}
